package com.zy.je.common.core.web.rest;

import com.zy.je.common.core.model.ICodeMessage;
import io.swagger.annotations.ApiModelProperty;

/**
 * @author zy
 */
public class RestResult<T> {

	@ApiModelProperty("是否成功")
	private boolean success;
	@ApiModelProperty("响应编码")
	private String code;
	@ApiModelProperty("数据内容")
	private T data;
	@ApiModelProperty("提示信息")
	private Object msg;
	@ApiModelProperty("详情描述")
	private String description;
	@ApiModelProperty("时间戳")
	private Long currentTime;

	public RestResult() {
	}

	public RestResult(boolean success, String code, T data, Object msg) {
		this.success = success;
		this.code = code;
		this.data = data;
		this.msg = msg;
		this.currentTime = System.currentTimeMillis();
	}

	public RestResult(ICodeMessage status, T data) {
		this(status, status.getMessage(), data,null);
	}

	public RestResult(ICodeMessage status, Object msg, T data) {
		this(status, msg,null,null);
	}
	public RestResult(ICodeMessage status, Object msg, T data, String description) {
		this(false, String.valueOf(status.getCode()), data, msg);
		this.success = ResultStatus.OK.getCode().equals(status.getCode());
		this.description = description;
	}

	public RestResult(ICodeMessage status) {
		this(status, null);
	}

	public static <T> RestResult<T> ok(T data, Object msg) {
		return new RestResult<T>(true, String.valueOf(ResultStatus.OK.getCode()), data, msg);
	}

	public static <T> RestResult<T> ok(T data) {
		return new RestResult<T>(true, String.valueOf(ResultStatus.OK.getCode()), data, "请求成功");
	}

	public static <T> RestResult<T> fail(T data, Object msg) {
		return new RestResult<T>(false, String.valueOf(ResultStatus.OK.getCode()), data, msg);
	}

	public boolean isSuccess() {
		return this.success;
	}

	public String getCode() {
		return this.code;
	}

	public T getData() {
		return this.data;
	}

	public Object getMsg() {
		return this.msg;
	}

	public Long getCurrentTime() {
		return this.currentTime;
	}

	public void setSuccess(final boolean success) {
		this.success = success;
	}

	public void setCode(final String code) {
		this.code = code;
	}

	public void setData(final T data) {
		this.data = data;
	}

	public void setMsg(final Object msg) {
		this.msg = msg;
	}

	public void setCurrentTime(final Long currentTime) {
		this.currentTime = currentTime;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public String toString() {
		return "RestResult(success=" + this.isSuccess() + ", code=" + this.getCode() + ", data=" + this.getData() + ", msg=" + this.getMsg() + ", currentTime=" + this.getCurrentTime() + ")";
	}
}
